package com.cxd.web.backend;

import com.cxd.entity.EbUser;
import com.cxd.service.IEbUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IEbUserService iEbUserService;

    @RequestMapping("/list")
    public String list(HttpSession session, Model model, @RequestParam(value = "page", defaultValue = "1") int psize, @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        List<EbUser> ebUsers = iEbUserService.listByPage(psize, 3);
        final Integer totalPage = iEbUserService.findTotalPage(pageSize);
        model.addAttribute("ebUsers", ebUsers);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currPage", psize);
        model.addAttribute("tag",2);
//        System.out.println("EbUserController==分页数据为：" + ebUsers);
//        System.out.println(model);
        return "manage/user";
    }

    /**
     * 实现分类项删除功能
     * model用于返回操作结果（影响行数）
     * epcId用于获得要删除项的id
     */
    @GetMapping("/delete")
    public String delete(HttpSession session,Model model, String euUserId) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        int num = iEbUserService.delete(euUserId);
        model.addAttribute("result", num);
        model.addAttribute("tag",2);
        return "manage/manage-result";
    }

    @RequestMapping("to_save")
    public String toSave(HttpSession session,Model model) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        model.addAttribute("tag",2);
        return "manage/user-add";
    }

    @RequestMapping("/save")
    public String save(HttpSession session,EbUser ebUser,Model model) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser userNow = (EbUser) user;
        if(userNow.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",userNow);
//        System.out.println("ebUser==>" + ebUser);
        ebUser.setEuStatus(2);
        int result = iEbUserService.save(ebUser);
        model.addAttribute("result", result);
        model.addAttribute("tag",2);
        return  "manage/manage-result";
    }


    @RequestMapping("/to_edit")
    public  String toEdit(HttpSession session,Model model,String euUserId){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser userNow = (EbUser) user;
        if(userNow.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",userNow);
        EbUser ebUser =iEbUserService.selectByCondition(euUserId);
        model.addAttribute("ebUser",ebUser);
        model.addAttribute("tag",2);
        return "manage/user-modify";
    }

    @RequestMapping("/edit")
    public  String edit(HttpSession session,EbUser ebUser,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser userNow = (EbUser) user;
        if(userNow.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",userNow);
        System.out.println(ebUser);
        int result =iEbUserService.updateByPrimaryKey(ebUser);
        model.addAttribute("result", result);
        model.addAttribute("tag",2);
       return "manage/manage-result";
    }
}
